<?php

namespace app\admin\model;

use think\Model;


class Buy extends Model
{

    

    

    // 表名
    protected $name = 'buy';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    //protected $deleteTime = false;

    public function getCreateTimeAttr($t)
    {
        return date('Y-m-d H:i:s', $t);
    }

    public function getUpdateTimeAttr($t)
    {
        return date('Y-m-d H:i:s', $t);
    }

    

    
    public function getExpressList()
    {
        return ['1' => __('Express 1'), '0' => __('Express 0')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getSoldTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sold_time']) ? $data['sold_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExpressTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['express']) ? $data['express'] : '');
        $list = $this->getExpressList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setSoldTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function classify()
    {
        return $this->belongsTo('Classify', 'classify_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function spec()
    {
        return $this->belongsTo('Spec', 'spec_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
